package com.honeyms.project.blog.close.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.honeyms.common.utils.R;
import com.honeyms.framework.web.controller.BaseController;
import com.honeyms.framework.web.page.BlogPageDomain;
import com.honeyms.framework.web.page.TableDataInfo;
import com.honeyms.project.blog.blogcomment.domain.BlogComment;
import com.honeyms.project.blog.blogcomment.service.IBlogCommentService;
import com.honeyms.project.blog.blogcontent.domain.BlogContent;
import com.honeyms.project.blog.blogcontent.service.IBlogContentService;
import com.honeyms.project.blog.blogcontentdetail.domain.BlogContentDetail;
import com.honeyms.project.blog.blogfavoritesusercontent.domain.BlogFavoritesUserContent;
import com.honeyms.project.blog.blogfavoritesusercontent.service.IBlogFavoritesUserContentService;
import com.honeyms.project.blog.userfans.domain.UserFans;
import com.honeyms.project.blog.userfans.domain.UserFansDetail;
import com.honeyms.project.blog.userfans.service.IUserFansService;
import com.honeyms.project.system.user.domain.User;
import com.honeyms.project.system.user.domain.UserEditPersonal;
import com.honeyms.project.system.user.service.IUserService;
import com.honeyms.project.system.userdetail.domain.UserDetail;
import com.honeyms.project.system.userdetail.service.IUserDetailService;

/**
 * 
 * @author honey
 * @version 创建时间：2020年10月29日 下午5:10:15
 */
@RequestMapping("/blog/user")
@Controller
public class BlogUserController extends BaseController{
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IUserDetailService userDetailService;
	
	@Autowired
	private IBlogContentService blogContentService;
	
	@Autowired
	private IBlogFavoritesUserContentService blogFavoritesUserContentService;
	
	@Autowired
	private IBlogCommentService blogCommentService;
	
	@Autowired
	private IUserFansService userFansService;
	
	//进入个人主页
	@GetMapping("/homepage")
	public String homepage(ModelMap mmap) {
		mmap.put("user", getSysUser());
		mmap.put("userdetail", userDetailService.selectUserDetailById(getUserId()));
		return "blog/index/homepage";
	}
	
	//用户签到
	@ResponseBody
	@PostMapping("/issign")
	public R issign() {
		UserDetail userDetail = userDetailService.selectUserDetailById(getUserId());
		if (userDetail.getIssign()==1) {
			return error(1,"你已经签过到了,明天再来吧!");
		}
		userDetail.setIssign(1);
		userDetail.setIntegrate(userDetail.getIntegrate()+1);
		if (userDetailService.updateUserDetail(userDetail)>0) {
			return ok(0, "签到成功!");
		}else {
			return error(1, "签到失败");
		}
	}
	
	//去我的关注界面
	@GetMapping("/follow")
	public String follow(ModelMap mmap) {
		if (getSysUser()!=null) {
			mmap.put("user",getSysUser());
		}
		//随机推荐
		BlogContent blogContent = new BlogContent();
    	blogContent.setStatus(1);
    	blogContent.setType(2);
		startPage(1,6);
		List<BlogContent> randContent = blogContentService.selectBlogContentListByRand(blogContent);
		mmap.put("randContent", randContent);
		mmap.put("guessyoulike", blogContentService.guessYouLike(getUserId()));
		return "blog/index/follow";
	}
	
	/**
	 * 主页获取我的帖子
	 * @param blogPageDomain
	 * @return
	 */
	@GetMapping("/homecontent")
	@ResponseBody
	public TableDataInfo homecontent(BlogPageDomain blogPageDomain) {
		BlogContent blogContent = new BlogContent();
		blogContent.setCreatedId(getUserId());
		blogContent.setStatus(1);
    	blogContent.setType(2);
		startPage(blogPageDomain.getPageNum(),blogPageDomain.getPageSize());
  		List<BlogContent> list = blogContentService.selectBlogContent(blogContent);
  		return getDataTable(list);
	}
	
	/**
	 * 主页获取我喜欢的帖子
	 * @param blogPageDomain
	 * @return
	 */
	@GetMapping("/homefavoritecontent")
	@ResponseBody
	public TableDataInfo homefavoritecontent(BlogPageDomain blogPageDomain) {
		BlogFavoritesUserContent blogFavoritesUserContent = new BlogFavoritesUserContent();
		blogFavoritesUserContent.setUserId(getUserId());
		startPage(blogPageDomain.getPageNum(),blogPageDomain.getPageSize());
		List<BlogContentDetail> list = blogFavoritesUserContentService.selectBlogContentDetailList(blogFavoritesUserContent);
		return getDataTable(list);
	}
	
	/**
	 * 主页获取我的评论
	 * @param blogPageDomain
	 * @return
	 */
	@GetMapping("/homecomment")
	@ResponseBody
	public TableDataInfo homecomment(BlogPageDomain blogPageDomain) {
		BlogComment blogComment = new BlogComment();
		blogComment.setUserId(getUserId());
		startPage(blogPageDomain.getPageNum(),blogPageDomain.getPageSize(),"comment_id DESC");
		List<BlogComment> list = blogCommentService.selectBlogCommentList(blogComment);
		return getDataTable(list);
	}
	
	/**
	 * 主页获取我的评论
	 * @param blogPageDomain
	 * @return
	 */
	@GetMapping("/fans")
	@ResponseBody
	public TableDataInfo fans(BlogPageDomain blogPageDomain) {
		UserFans userFans = new UserFans();
		userFans.setUserId(getUserId());
		userFans.setFansId(getUserId());
		startPage(blogPageDomain.getPageNum(),blogPageDomain.getPageSize());
		List<UserFansDetail> list = userFansService.selectUserFans(userFans);
		return getDataTable(list);
	}
	
	/**
	 * 修改自己基本信息
	 * @param userEditPersonal
	 * @return
	 */
	@PostMapping("/editPersonal")
	@ResponseBody
	@Transactional
	public R editPersonal(UserEditPersonal userEditPersonal) {
		if (getUserId().longValue()!=userEditPersonal.getUserId().longValue()) {
			return error(2,"请修改自己的账号!");
		}
		User user = new User();
		user.setUserId(userEditPersonal.getUserId());
		user.setUserName(userEditPersonal.getUserName());
		user.setEmail(userEditPersonal.getEmail());
		user.setPhonenumber(userEditPersonal.getPhonenumber());
		user.setSex(userEditPersonal.getSex());
		user.setRemark(userEditPersonal.getRemark());
		int row = userService.updateUserInfo(user);
		if(row>0) {
			UserDetail userDetail = userDetailService.selectUserDetailById(getUserId());
			userDetail.setIntegrate(userDetail.getIntegrate()-1);
			if(userDetailService.updateUserDetail(userDetail)>0) {
				return ok(1,"操作成功,将会在下次登录后生效");
			}
		}
		return error(2,"未知异常");
	}
	
}
